<?php

namespace Api\Controller;

/**
 * 艺术社群
 *
 * Class GroupController
 * @package Api\Controller
 */
class GroupController extends PublicController
{
    private $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = M('group');
    }

    /**
     * 获取艺术社群分页数据
     */
    public function getList()
    {
        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $groupType = in_array(intval($_GET['type']), [1, 2]) ? intval($_GET['type']) : 1;
        $where = "is_del = 0 and group_type = " . $groupType;

        $count = $this->model->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = $this->model->where($where)->field('id, logo, title,cover,brief,sort')->order('sort = 0, sort asc, id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $list[$k]['cover'] = __DATAURL__ . $v['cover'];
            !empty($v['logo']) && $list[$k]['logo'] = __DATAURL__ . $v['logo'];
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 获取艺术社群详情
     */
    public function detail()
    {
        $id = intval($_REQUEST['id']);
        $detail = $this->model->where(['id' => $id])->find();
        if (!$detail) {
            $this->json_error('没有找到相关信息');
        }

        $detail['content'] = str_replace(['../../../Data/', "/Data/"], ['/Data/', __DATAURL__], $detail['content']);
        $detail['cover'] = __DATAURL__ . $detail['cover'];
        !empty($detail['logo']) && $detail['logo'] = __DATAURL__ . $detail['logo'];
        $detail['apply'] = 0;

        // 当前用户是否已报名
        if ($userId = intval($this->get("id"))) {
            if ($apply = M('group_apply')->where(['group_id' => $id, 'user_id' => $userId])->find()) {
                $detail['apply'] = 1;
                $detail['apply_id'] = $apply['id'];
            }
        }

        $this->json_result($detail);
    }

    /**
     * 活动预约申请
     */
    public function apply()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $fields = [
            'group_id' => '招募id',
            'name'     => '姓名',
            'mobile'   => '手机号码',
            'photo'    => '照片',
            'birthday' => '出生日期',
            'height'   => '身高',
            'talent'   => '才艺',
            'identity' => '身份',
            'form_id'  => '表单id',
        ];

        foreach ($fields as $key => $name) {
            if (empty($_POST[$key])) {
                $this->json_error("参数错误：请输入" . $name);
            }
        }

        $data = [
            'group_id'  => intval($_POST['group_id']),
            'user_id'   => $userId,
            'name'      => $_POST['name'],
            'mobile'    => $_POST['mobile'],
            'photo'     => $_POST['photo'],
            'gender'    => $_POST['gender'],
            'birthday'  => $_POST['birthday'],
            'height'    => $_POST['height'],
            'talent'    => $_POST['talent'],
            'identity'  => $_POST['identity'],
            'form_id'   => $_POST['form_id'],
            'create_at' => date('Y-m-d H:i:s'),
        ];

        $groupId = $this->model->where('is_del = 0 and id=' . $_POST['group_id'])->getField('id');
        if (empty($groupId)) {
            $this->json_error("数据错误：招募活动不存在");
        }

        if (M('group_apply')->where(['group_id' => $data['group_id'], 'user_id' => $data['user_id']])->find()) {
            $this->json_error('请勿重复报名');
        }

        $result = M('group_apply')->add($data);
        if ($result) {
            $this->json_result([], '操作成功');
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 我的活动预约
     */
    public function myApply()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $groupType = in_array(intval($_GET['type']), [1, 2]) ? intval($_GET['type']) : 1;
        $where = "user_id=" . $userId . ' and group_type = ' . $groupType;

        $count = M('group_apply')->where($where)->join('lr_group on lr_group.id = lr_group_apply.group_id')->count();
        $totalPage = ceil($count / $perPage);
        $list = M('group_apply')->where($where)->join('lr_group on lr_group.id = lr_group_apply.group_id')
            ->field('lr_group_apply.*, lr_group.title, lr_group.cover,lr_group.logo,lr_group.brief,lr_group.group_type')->order('lr_group_apply.id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $list[$k]['cover'] = __DATAURL__ . $v['cover'];
            !empty($v['logo']) && $list[$k]['logo'] = __DATAURL__ . $v['logo'];
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);

    }

    /**
     * 艺术社群报名详情
     */
    public function applyDetail()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $applyId = intval($_GET['id']);
        if (!$applyId) {
            $this->json_error("参数错误");
        }

        $apply = M('group_apply')->where(['user_id' => $userId, 'id' => $applyId])->find();
        if (empty($apply)) {
            $this->json_error("数据错误");
        }

        !empty($apply['photo']) && $apply['photo'] = __DATAURL__ . $apply['photo'];

        $apply['group'] = M('group')->where(['id' => $apply['group_id']])->find();
        !empty($apply['group']['cover']) && $apply['group']['cover'] = __DATAURL__ . $apply['group']['cover'];
        !empty($apply['group']['logo']) && $apply['group']['logo'] = __DATAURL__ . $apply['group']['logo'];

        $this->json_result($apply);
    }
}